package cn.itcast.tanhua.mytanhuasso.service.impl;

import cn.itcast.tanhua.enums.SexEnum;
import cn.itcast.tanhua.mytanhuasso.mapper.UserInfoMapper;
import cn.itcast.tanhua.mytanhuasso.service.UserInfoService;
import cn.itcast.tanhua.mytanhuasso.service.UserService;
import cn.itcast.tanhua.pojo.User;
import cn.itcast.tanhua.pojo.UserInfo;
import cn.itcast.tanhua.utils.UserThreadLocal;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.aliyunpicuploadspringbootstart.service.PicUploadService;
import com.itheima.aliyunpicuploadspringbootstart.vo.PicUploadResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author 郭文亮
 * @since 2021/11/24 9:20
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private FaceEngineService faceEngineService;

    @Autowired
    private PicUploadService picUploadService;

    @Override
    public Boolean saveUserInfo(Map<String, String> param, String token) {
        // 1.通过token，获取user信息
        User userByToken = UserThreadLocal.get();

        String sex = param.get("gender");
        String nickname = param.get("nickname");
        String birthday = param.get("birthday");
        String city = param.get("city");
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userByToken.getId());
        userInfo.setSex(StringUtils.equalsIgnoreCase(sex, "man") ? SexEnum.MAN : SexEnum.WOMAN);
        userInfo.setNickName(nickname);
        userInfo.setBirthday(birthday);
        userInfo.setCity(city);
        // 将用户信息写入tb_user_info表
        int insertResult = userInfoMapper.insert(userInfo);

        return insertResult == 1;
    }

    @Override
    public Boolean updateUserInfoLogo(MultipartFile multipartFile, String token) {
        // 校验token
        // 通过token查询出user的信息，user里包含了user的id，mobile
        User userByToken = userService.queryUserByToken(token);
        if (null == userByToken) {
            return false;
        }
        // 检验图片是否为人像
        try {
            boolean isPortrait = this.faceEngineService.checkIsPortrait(multipartFile.getBytes());
            if (!isPortrait) {
                // 不是人像，返回false
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 是人像，上传到阿里云oss
        PicUploadResult picUploadResult = this.picUploadService.upload(multipartFile);
        if (StringUtils.isEmpty(picUploadResult.getName())) {
            //上传失败
            return false;
        }
        // 上传成功，将图片地址传到mysql
        UserInfo userInfo = new UserInfo();
        userInfo.setLogo(picUploadResult.getName());
        LambdaQueryWrapper<UserInfo> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.eq(UserInfo::getUserId,userByToken.getId());
        int updateResult = this.userInfoMapper.update(userInfo, updateWrapper);
        return updateResult == 1;
    }
}
